package carmesi.wizards;

import java.awt.Component;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.netbeans.api.java.project.JavaProjectConstants;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.SourceGroup;
import org.netbeans.api.project.Sources;
import org.netbeans.spi.java.project.support.ui.templates.JavaTemplates;
import org.netbeans.spi.project.ui.templates.support.Templates;
import org.openide.WizardDescriptor.Panel;

/**
 *
 * @author Victor
 */
public class ConverterWizardIterator extends CarmesiWizardIterator implements ComponentValidator<ConverterComponent> {
    private ConverterComponent component;

    @Override
    protected Panel[] createPanels() {
        Project project = Templates.getProject(getWizardDescriptor());
        Sources sources = project.getLookup().lookup(Sources.class);
        SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
        component=new ConverterComponent(getWizardDescriptor());
        component.setComponentValidator(this);
        return new Panel[]{JavaTemplates.createPackageChooser(project, groups, component)};
    }

    @Override
    protected void setTemplateArguments(Map<String, String> args) {
        args.put("class", component.getClassname());
        String simpleClassname=component.getClassname();
        int index=simpleClassname.lastIndexOf('.');
        if(index != -1 && index < simpleClassname.length() - 1){
            simpleClassname=simpleClassname.substring(index+1);
        }
        args.put("simpleClassname", simpleClassname);
    }

    @Override
    public void validate(ConverterComponent panel) throws IllegalArgumentException {
        if(panel.getClassname().trim().equals("")){
            throw new IllegalArgumentException("Class must not be empty");
        }
    }
    
}
